# API测试项目 Makefile
# 提供常用的开发和测试命令

.PHONY: help install install-dev clean test test-unit test-integration test-api test-coverage lint format check run server docs build

# 默认目标
help:
	@echo "API测试项目 - 可用命令:"
	@echo ""
	@echo "安装和环境:"
	@echo "  install      - 安装项目依赖"
	@echo "  install-dev  - 安装开发依赖"
	@echo "  clean        - 清理临时文件和缓存"
	@echo ""
	@echo "测试:"
	@echo "  test         - 运行所有测试"
	@echo "  test-unit    - 运行单元测试"
	@echo "  test-integration - 运行集成测试"
	@echo "  test-api     - 运行API测试"
	@echo "  test-coverage - 运行测试并生成覆盖率报告"
	@echo "  test-performance - 运行压力测试"
	@echo "  test-load    - 运行负载测试"
	@echo "  test-stress  - 运行压力测试"
	@echo "  test-spike   - 运行峰值测试"
	@echo ""
	@echo "代码质量:"
	@echo "  lint         - 运行代码检查"
	@echo "  format       - 格式化代码"
	@echo "  check        - 运行所有检查（格式化+检查）"
	@echo ""
	@echo "服务:"
	@echo "  run          - 启动API服务器"
	@echo "  server       - 启动API服务器（别名）"
	@echo ""
	@echo "文档和构建:"
	@echo "  docs         - 生成API文档"
	@echo "  build        - 构建项目"

# 安装依赖
install:
	@echo "安装项目依赖..."
	pip install -r requirements.txt

install-dev:
	@echo "安装开发依赖..."
	pip install -e ".[dev,test]"
	pre-commit install

# 清理
clean:
	@echo "清理临时文件和缓存..."
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	rm -rf build/
	rm -rf dist/
	rm -rf .pytest_cache/
	rm -rf .coverage
	rm -rf htmlcov/
	rm -rf allure-results/
	rm -rf allure-report/
	rm -rf reports/
	rm -rf logs/

# 测试
test:
	@echo "运行所有测试..."
	pytest tests/ -v --alluredir=allure-results

test-unit:
	@echo "运行单元测试..."
	pytest tests/ -m "unit" -v

test-integration:
	@echo "运行集成测试..."
	pytest tests/ -m "integration" -v

test-api:
	@echo "运行API测试..."
	pytest tests/ -m "api" -v --alluredir=allure-results

test-coverage:
	@echo "运行测试并生成覆盖率报告..."
	pytest tests/ --cov=app --cov=tests --cov-report=html --cov-report=term-missing

# 压力测试
test-performance:
	@echo "运行压力测试（负载测试）..."
	python tests/performance/run_performance_test.py --scenario load --environment local

test-load:
	@echo "运行负载测试..."
	python tests/performance/run_performance_test.py --scenario load --environment local

test-stress:
	@echo "运行压力测试..."
	python tests/performance/run_performance_test.py --scenario stress --environment local

test-spike:
	@echo "运行峰值测试..."
	python tests/performance/run_performance_test.py --scenario spike --environment local

test-endurance:
	@echo "运行持久性测试..."
	python tests/performance/run_performance_test.py --scenario endurance --environment local

test-smoke-performance:
	@echo "运行冒烟压力测试..."
	python tests/performance/run_performance_test.py --scenario smoke --environment local

# 压力测试分析
performance-analyze:
	@echo "分析最新的压力测试结果..."
	python tests/performance/analyzer.py

performance-compare:
	@echo "比较多个压力测试结果..."
	python tests/performance/analyzer.py --compare --limit 5

# Locust Web界面
locust-web:
	@echo "启动Locust Web界面..."
	locust -f tests/performance/locustfile.py --host=http://localhost:8000

# 代码质量
lint:
	@echo "运行代码检查..."
	flake8 app tests utils
	mypy app tests utils

format:
	@echo "格式化代码..."
	black app tests utils
	isort app tests utils

check: format lint
	@echo "所有检查完成"

# 服务
run:
	@echo "启动API服务器..."
	python main.py

server: run

# 开发服务器（自动重载）
dev:
	@echo "启动开发服务器（自动重载）..."
	uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload

# 文档
docs:
	@echo "生成API文档..."
	@echo "API文档可在以下地址访问:"
	@echo "  Swagger UI: http://localhost:8000/docs"
	@echo "  ReDoc: http://localhost:8000/redoc"
	@echo "请先启动服务器: make run"

# Allure报告
allure-serve:
	@echo "启动Allure报告服务器..."
	allure serve allure-results

allure-generate:
	@echo "生成Allure报告..."
	allure generate allure-results -o allure-report --clean

allure-open:
	@echo "打开Allure报告..."
	python scripts/generate_allure_report.py open

allure-clean:
	@echo "清理Allure文件..."
	python scripts/generate_allure_report.py clean --all

allure-check:
	@echo "检查Allure安装..."
	python scripts/generate_allure_report.py check

allure-report:
	@echo "生成并打开Allure报告..."
	python scripts/generate_allure_report.py generate --open

allure-history:
	@echo "生成带历史趋势的Allure报告..."
	@if exist allure-report\history (xcopy allure-report\history allure-results\history /E /I /Y)
	allure generate allure-results -o allure-report --clean

# 构建
build:
	@echo "构建项目..."
	python -m build

# Docker相关（如果需要）
docker-build:
	@echo "构建Docker镜像..."
	docker build -t api-test-demo .

docker-run:
	@echo "运行Docker容器..."
	docker run -p 8000:8000 api-test-demo

# 数据库相关（如果需要）
db-init:
	@echo "初始化数据库..."
	# 添加数据库初始化命令

db-migrate:
	@echo "运行数据库迁移..."
	# 添加数据库迁移命令

# 部署相关
deploy-staging:
	@echo "部署到测试环境..."
	# 添加部署命令

deploy-prod:
	@echo "部署到生产环境..."
	# 添加部署命令

# 监控和日志
logs:
	@echo "查看应用日志..."
	tail -f logs/api_test.log

monitor:
	@echo "监控应用状态..."
	# 添加监控命令